<template>
	<div>
		<div class="box">
			<uni-forms
				ref="form"
				:model="plan"
				validate-trigger="bind"
				:label-position="state == 'write' ? 'top' : 'left'"
				label-width="auto">
				<FromItem label="选择车辆" :required="true" :state="state" name="carId">
					<Car
						:validate="v"
						@sure="sure"
						:label="plan.carModelName"
						:state="state"
						:params="{
							quotationProductSchemeId: details.schemeId,
							businessType: 7,
							carLockType: 2,
						}"></Car>
				</FromItem>
				<FromItem label="车牌号" :state="state" name="carPlateNo">
					{{ plan.carPlateNo || '-' }}
				</FromItem>
				<FromItem label="车辆种类" :state="state" name="carTypeStr">
					{{ plan.carTypeStr || '-' }}
				</FromItem>
				<FromItem label="车辆类型" :state="state" name="carPowerTypeStr">
					{{ plan.carPowerTypeStr || '-' }}
				</FromItem>
				<FromItem label="品牌" :state="state" name="carBrandName">
					{{ plan.carBrandName || '-' }}
				</FromItem>
				<FromItem label="车系" :state="state" name="carSeriesName">
					{{ plan.carSeriesName || '-' }}
				</FromItem>
				<FromItem label="车型" :state="state" name="carModelName">
					{{ plan.carModelName || '-' }}
				</FromItem>
				<FromItem label="厂商" :state="state" name="carManufacturer">
					{{ plan.carManufacturer || '-' }}
				</FromItem>
				<FromItem label="座位" :state="state" name="carSeating">
					<div v-if="showSelectSeat">
						<ASelectUni
							:dict="{
								list: selectSeatList,
							}"
							:state="state"
							v-model="plan.carSeating"></ASelectUni>
					</div>
					<div v-else>
						{{ plan.carSeating || '-' }}
					</div>
				</FromItem>
				<FromItem label="排量" :state="state" name="carCc">
					{{ plan.carCc || '-' }}
				</FromItem>
				<FromItem label="车辆使用性质" :state="state" :required="true" name="operationType">
					<ASelectUni
						:dict="{
							list: dict.type.quota_operation_type,
						}"
						:state="state"
						v-model="plan.operationType"></ASelectUni>
				</FromItem>
				<FromItem label="原新车采购价" :state="state" name="carPurchasePriceQuondam">
					{{ A_viewPrice(plan.carPurchasePriceQuondam) || '-' }}
				</FromItem>
				<FromItem label="首次登记日期" :state="state" name="carFirstRegistrationDate">
					{{ plan.carFirstRegistrationDate || '-' }}
				</FromItem>
				<FromItem label="公里" :state="state" name="carMileage">
					{{ plan.carMileage || '-' }}
				</FromItem>
				<FromItem label="产权城市" :state="state" name="carPropertyCity">
					{{ plan.carPropertyCity || '-' }}
				</FromItem>
				<FromItem label="所属公司" :state="state" :required="true" name="carAffiliationKey">
					<ASelectUni
						:dict="{
							list: deptList.company,
							label: 'deptName',
							value: 'deptId',
						}"
						:state="state"
						@change="change_carAffiliationKey"
						v-model="plan.carAffiliationKey"></ASelectUni>
				</FromItem>
				<FromItem label="运营城市" :state="state" :required="true" name="carOperateCityVal">
					<AInput :state="state" v-model="plan.carOperateCityVal"></AInput>
				</FromItem>
				<FromItem
					label="当期评估价"
					:state="state"
					:required="true"
					name="carCurrentAppraisal">
					<AInput :state="state" type="price" v-model="plan.carCurrentAppraisal"></AInput>
				</FromItem>
				<FromItem
					label="到期残值"
					:state="state"
					:required="true"
					name="carSurplusAppraisal">
					<AInput :state="state" type="price" v-model="plan.carSurplusAppraisal"></AInput>
				</FromItem>
				<FromItem label="GPS费用" :state="state_gpsFee" name="gpsFee" :required="true">
					<AInput
						:state="state_gpsFee"
						type="price"
						@change="change_gpsFee"
						v-model="plan.gpsFee"></AInput>
				</FromItem>
				<FromItem label="年检费（年）" state="readonly" name="annualCheckFee">
					<AInput state="readonly" type="price" v-model="plan.annualCheckFee"></AInput>
				</FromItem>
				<FromItem label="3年内出险次数" :state="state" name="carLossFrequence">
					{{ plan.carLossFrequence || '-' }}
				</FromItem>
			</uni-forms>
		</div>
	</div>
</template>

<script>
	import * as app from '@/api/index.js';
	import * as apis from '@/api/quota/apply/api.js';
	import * as utils from '@/pages/quota/libs/utils.js';
	import FromItem from '../formItem/index.vue';
	import Car from '@/pages/quota/AForm/car/index.vue';
	export default {
		name: '',
		components: { FromItem, Car },
		dicts: ['quota_operation_type'],
		props: {},
		inject: [
			'details_provide',
			'config_provide',
			'deptList_provide',
			'state_provide',
			'index_provide',
			'planDicts_provide',
			'fromtype_provide',
		],
		data() {
			return {};
		},
		computed: {
			details() {
				return this.details_provide();
			},
			config() {
				return this.config_provide();
			},
			deptList() {
				return this.deptList_provide();
			},
			planDicts() {
				return this.planDicts_provide();
			},
			state() {
				return this.state_provide();
			},
			fromtype() {
				return this.fromtype_provide();
			},
			index() {
				return this.index_provide();
			},
			currentInfoData() {
				return this.details.productList?.[this.index] ?? {};
			},
			plan() {
				return this.currentInfoData?.plan ?? {};
			},
			firstInfoData() {
				return this.details.productList?.[0] ?? {};
			},
			state_gpsFee() {
				return this.state == 'readonly'
					? 'readonly'
					: this.config?.quotationProductSchemeRule?.openGpsModification == 1
					? 'write'
					: 'readonly';
			},
			v() {
				if (!this.details?.schemeId) {
					return false;
				}
				return true;
			},
			showSelectSeat() {
				return /(\-|\/)/g.test(this.plan.carSeatingType);
			},
			selectSeatList() {
				if (!this.plan?.carSeatingType) {
					return [];
				}
				if (this.plan.carSeatingType.indexOf('/') !== -1) {
					return this.plan.carSeatingType?.split('/');
				}
				if (this.plan.carSeatingType.indexOf('-') !== -1) {
					let arr = this.plan.carSeatingType?.split('-');
					if (arr?.length < 2) return [];
					let start = arr?.[0] ?? 1;
					let end = arr?.[1];
					let seat = [];
					for (let index = start; index <= end; index++) {
						seat.push(index);
					}
					return seat;
				}
			},
		},
		watch: {},
		filters: {},
		created() {},
		mounted() {},
		methods: {
			getPrevInsurance(car) {
				if (!car.carId) return;
				let params = { carId: car.carId };
				apis.LastInsuranceMoney(params).then(res => {
					console.log(res);

					if (res.code == 200) {
						// 交强险测算金额
						this.currentFormData.insuranceCompulsoryPrevAmount =
							res.data.compulsoryTotalMoney;
						// 车船税测算金额
						this.currentFormData.insuranceCarvesselPrevAmount =
							res.data.carTaxTotalMoney;
						// 商业险测算金额
						this.currentFormData.insuranceCommercialPrevAmount =
							res.data.commercialTotalMoney;

						// 交强险测算金额
						this.currentFormData.insuranceCompulsoryCurrentAmount =
							res.data.compulsoryTotalMoney;

						this.currentFormData.planConf.insuranceCompulsoryCurrentAmount_state =
							'changed';
						// 车船税测算金额
						this.currentFormData.insuranceCarvesselCurrentAmount =
							res.data.carTaxTotalMoney;

						this.currentFormData.planConf.insuranceCarvesselCurrentAmount_state =
							'changed';

						// 商业险测算金额
						this.currentFormData.insuranceCommercialCurrentAmount =
							res.data.commercialTotalMoney;

						this.currentFormData.planConf.insuranceCommercialCurrentAmount_state =
							'changed';
					}
				});
			},
			sure(val) {
				this.setCarData(val.item);
				this.$emit('getRefittedList');
				this.$emit('initInputState');
				this.$emit('initCalcData');
			},
			setCarData(res = {}) {
				console.log(res);
				// 内饰颜色
				this._set('interiorColor', res.interiorColor);
				//外观颜色
				this._set('appearanceColor', res.exteriorColor);
				//车辆id
				this._set('carId', res.carId);
				//车牌号
				this._set('carPlateNo', res.carNo);
				// 车架号
				this._set('carVinNo', res.vinNo);
				//车型id
				this._set('carModelId', res.modelId);
				//车型
				this._set('carModelName', res.modelName);
				//品牌
				this._set('carBrandName', res.brandName);
				//品牌id
				this._set('carBrandId', res.brandId);
				//车系
				this._set('carSeriesName', res.seriesName);
				//车系id
				this._set('carSeriesId', res.seriesId);
				// 座位数
				this._set('carSeatingType', res.seatNum);
				if (/(\-|\/)/g.test(res.seatNum)) {
					this._set('carSeating', undefined);
				} else {
					this._set('carSeating', res.seatNum);
				}
				// 排量
				this._set('carCc', res.liter);
				// 车辆种类
				this._set('carTypeStr', res.vehicleCategory);
				// 车辆类型
				if (this.notEmpty(res.isGreen)) {
					this._set('carPowerTypeStr', res.isGreen == 1 ? '新能源车' : '燃油车');
				}
				// 厂商
				this._set('carManufacturer', res.makerName);
				let price = res.price * 10000;
				// 厂商指导价
				this._set(
					'carGuidePrice',
					Number.isFinite(price) // 厂商指导价
						? price
						: undefined,
				);
				// 是否新能源
				this._set('isGreen', res.isGreen);
				// 公里
				this._set('carMileage', res.mileage);
				// 产权城市
				this._set('carPropertyCity', res.propertyRightName);

				// 上牌城市ID
				if (this.Empty(this.currentInfoData.carLicenseCityKey)) {
					// 上牌城市
					this.currentInfoData.carLicenseCityKey = res.propertyRightId;
					// 开发城市
					this.currentInfoData.carAffiliationKey = res.propertyRightId;
					let item = this.deptList.company.find(e => e.deptId == res.propertyRightId);
					if (item) {
						// 上牌城市
						this.currentInfoData.carLicenseCityVal = item.deptName;
						let regionId = item.regionId.split('/');
						this.currentInfoData.carCalcCityKey = regionId?.[1];
						// 开发城市
						this.currentInfoData.carAffiliationVal = item?.deptName;
					}
				}
				// 首次登记日期
				this._set('carFirstRegistrationDate', res.registerTime);
				// 原新车采购价
				this._set('carPurchasePriceQuondam', res.purchasePrice);
				this.getInsCount(res.carId);
				this.getPrevInsurance(res);
				if (res.isGreen == 1) {
					this.$emit('setInsuranceCarvesselCurrentAmount');
				}
			},
			_set(prop, data) {
				this.$set(this.plan, prop, data);
			},
			getPrevInsurance(car) {
				if (!car.carId) return;
				let params = { carId: car.carId };
				apis.LastInsuranceMoney(params).then(res => {
					console.log(res);

					if (res.code == 200) {
						// 交强险测算金额

						this.$set(
							this.currentInfoData,
							'insuranceCompulsoryPrevAmount',
							res.data.compulsoryTotalMoney,
						);
						// 车船税测算金额

						this.$set(
							this.currentInfoData,
							'insuranceCarvesselPrevAmount',
							res.data.carTaxTotalMoney,
						);
						// 商业险测算金额

						this.$set(
							this.currentInfoData,
							'insuranceCommercialPrevAmount',
							res.data.commercialTotalMoney,
						);
						// 交强险测算金额

						this.$set(
							this.currentInfoData,
							'insuranceCompulsoryCurrentAmount',
							res.data.compulsoryTotalMoney,
						);
						this.currentInfoData.planConf.insuranceCompulsoryCurrentAmount_state =
							'changed';
						// 车船税测算金额

						this.$set(
							this.currentInfoData,
							'insuranceCarvesselCurrentAmount',
							res.data.carTaxTotalMoney,
						);
						this.currentInfoData.planConf.insuranceCarvesselCurrentAmount_state =
							'changed';

						// 商业险测算金额

						this.$set(
							this.currentInfoData,
							'insuranceCommercialCurrentAmount',
							res.data.commercialTotalMoney,
						);
						this.currentInfoData.planConf.insuranceCommercialCurrentAmount_state =
							'changed';
					}
				});
			},
			// 获取三年出现次数
			getInsCount(carId) {
				if (!carId) return;
				apis.CarCount(carId).then(res => {
					if (res.code == 200) {
						this._set('carLossFrequence', res.data);
					}
				});
			},
			change_carAffiliationKey({ val, item }) {
				this.plan.carAffiliationVal = item.deptName;
			},
			change_gpsFee(val) {
				if (this.notEmpty(val)) {
					this.currentInfoData.planConf.gpsFee_state = 'changed';
				} else {
					this.currentInfoData.planConf.gpsFee_state = '';
				}
			},
		},
		beforeDestroy() {
			if (this.notEmpty(this.details?.productList?.[this.index])) {
				this._resetFields({
					data: this.plan,
				});
			}
		},
	};
</script>

<style lang="scss" scoped>
	.box {
		// padding: 20rpx;
		padding-top: 20rpx;
		background-color: #fff;
		border-radius: 8rpx;
		.title {
			border-bottom: 1px solid #f5f5f5;
			padding-bottom: 10rpx;
			margin-bottom: 10rpx;
		}
	}
</style>
